package com.ilop.sthome.ui.adapter.detail;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.ilop.sthome.data.greenDao.SceneBean;
import com.ilop.sthome.utils.CoderUtils;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020/4/15.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：选择映射场景
 */
public class MappingAdapter extends RecyclerView.Adapter<MappingAdapter.ItemHolder> {

    private Context mContext;
    private List<SceneBean> mList;
    private List<String> sceneList;
    private String mMapping;

    public MappingAdapter(Context mContext, String deviceName, String mMapping) {
        this.mContext = mContext;
        this.mMapping = mMapping;
        if (!"0FFF".equals(mMapping)) {
            sceneList = CoderUtils.getSceneList(mMapping);
        }else {
            sceneList = CoderUtils.getSceneList(CoderUtils.getAllMappingScene(deviceName));
        }
    }

    public void setList(List<SceneBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public List<String> getSceneList() {
        return sceneList;
    }

    @NonNull
    @Override
    public MappingAdapter.ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_mapping, viewGroup, false);
        return new MappingAdapter.ItemHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull MappingAdapter.ItemHolder itemHolder, int i) {
        SceneBean mBean = mList.get(i);
        switch (mBean.getSid()) {
            case 0:
                itemHolder.mIcon.setImageResource(R.mipmap.scene_at_home);
                itemHolder.mName.setText(mContext.getString(R.string.home_mode));
                break;
            case 1:
                itemHolder.mIcon.setImageResource(R.mipmap.scene_leave_home);
                itemHolder.mName.setText(mContext.getString(R.string.out_mode));
                break;
            case 2:
                itemHolder.mIcon.setImageResource(R.mipmap.scene_sleep);
                itemHolder.mName.setText(mContext.getString(R.string.sleep_mode));
                break;
            default:
                itemHolder.mIcon.setImageResource(R.mipmap.scene_custom);
                itemHolder.mName.setText(mBean.getModleName());
                break;
        }
        if ("0FFF".equals(mMapping)){
            itemHolder.mChange.setChecked(true);
        }else {
            if (sceneList.contains(String.valueOf(mBean.getSid()))) {
                itemHolder.mChange.setChecked(true);
            } else {
                itemHolder.mChange.setChecked(false);
            }
        }
        View.OnClickListener scene_change = view ->{
            if (itemHolder.mChange.isChecked()){
                itemHolder.mChange.setChecked(false);
                sceneList.remove(String.valueOf(mBean.getSid()));
            }else {
                itemHolder.mChange.setChecked(true);
                sceneList.add(String.valueOf(mBean.getSid()));
            }
        };
        itemHolder.mChange.setOnClickListener(v -> {
            if (itemHolder.mChange.isChecked()){
                sceneList.add(String.valueOf(mBean.getSid()));
            }else {
                sceneList.remove(String.valueOf(mBean.getSid()));
            }
        });
        itemHolder.itemView.setOnClickListener(scene_change);
    }


    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_scene_icon)
        ImageView mIcon;
        @BindView(R.id.tv_scene_name)
        TextView mName;
        @BindView(R.id.iv_scene_change)
        CheckBox mChange;

        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
